package com.cx.androidtools;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.AnimationUtils;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class ViewFlipperActivity extends Activity implements OnGestureListener {
	private ViewFlipper mViewFlipper;
	private GestureDetector mGestureDetector;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_flipper_layout);
		mGestureDetector = new GestureDetector(this);
		mViewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
		mViewFlipper.addView(addView(R.drawable.view_filpper_01));
		mViewFlipper.addView(addView(R.drawable.view_filpper_02));
		mViewFlipper.addView(addView(R.drawable.view_filpper_03));
		mViewFlipper.addView(addView(R.drawable.view_filpper_04));
//		mViewFlipper.addView(addView(R.drawable.view_filpper_05));
	}

	private View addView(int Resource) {
		ImageView mImageView = new ImageView(this);
		mImageView.setImageResource(Resource);
		return mImageView;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return this.mGestureDetector.onTouchEvent(event);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (e1.getX() - e2.getX() > 120) {
			this.mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_in));
			this.mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
					this, R.anim.push_left_out));
			this.mViewFlipper.showNext();
			return true;
		} else if (e1.getX() - e2.getX() < -120) {
			this.mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_in));
			this.mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
					this, R.anim.push_right_out));
			this.mViewFlipper.showPrevious();
			return true;
		}else
		return false;
	}
}
